#!/usr/bin/env python3

import numpy as np
import cv2 as cv

img = cv.imread("../pic/7.png", 1)

# 根据坐标访问像素值
px = img[100, 100]
print(px)

# 只访问BGR的B通道
blue = img[100, 100, 0]
print(blue)

# 改变该点值
img[100, 100] = [255, 255, 255]
print(img[100, 100])

# 更快速的访问和修改：使用item和itemset
print(img.item(10, 10, 2))

# 修改 RED 值
img.itemset((10, 10, 0), 100)
print(img.item(10, 10, 2))

# (568, 442, 3).行列 通道数。如果 灰度图则只有行列数
sp = img.shape
print(sp)

# 像素个数
sz = img.size
print(sz)
# 像素类型
print(img.dtype)

print('-----------------------------------')
# 选定区域
cat = img[270:400, 100:190]
# 将cat区域复制到图像中的另一个区域
img[100:230, 20:110] = cat

# 拆分合并
b, g, r = cv.split(img)  # 相对耗时
img = cv.merge((b, g, r))

img[:, :, 2] = 0

cv.namedWindow("image")

cv.imshow('image', img)

cv.waitKey(0)

# 图像边框
'''
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv.imread('../pic/7.png')

replicate = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)

plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()

'''
